{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if Array is Good"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isGood"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查数组是否是好的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;，如果它是数组&nbsp;<code>base[n]</code>&nbsp;的一个排列，我们称它是个&nbsp;<strong>好</strong>&nbsp;数组。</p>\n",
    "\n",
    "<p><code>base[n] = [1, 2, ..., n - 1, n, n]</code>&nbsp;（换句话说，它是一个长度为 <code>n + 1</code>&nbsp;且包含&nbsp;<code>1</code>&nbsp;到&nbsp;<code>n - 1</code>&nbsp;恰好各一次，包含 <code>n</code>&nbsp; 两次的一个数组）。比方说，<code>base[1] = [1, 1]</code>&nbsp;，<code>base[3] = [1, 2, 3, 3]</code>&nbsp;。</p>\n",
    "\n",
    "<p>如果数组是一个好数组，请你返回&nbsp;<code>true</code>&nbsp;，否则返回&nbsp;<code>false</code>&nbsp;。</p>\n",
    "\n",
    "<p><strong>注意：</strong>数组的排列是这些数字按任意顺序排布后重新得到的数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [2, 1, 3]\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>因为数组的最大元素是 3 ，唯一可以构成这个数组的 base[n] 对应的 n = 3 。但是 base[3] 有 4 个元素，但数组 nums 只有 3 个元素，所以无法得到 base[3] = [1, 2, 3, 3] 的排列，所以答案为 false 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1, 3, 3, 2]\n",
    "<b>输出：</b>true\n",
    "<b>解释：因为</b>数组的最大元素是 3 ，唯一可以构成这个数组的 base[n] 对应的 n = 3 ，可以看出数组是 base[3] = [1, 2, 3, 3] 的一个排列（交换 nums 中第二个和第四个元素）。所以答案为 true 。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1, 1]\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>因为数组的最大元素是 1 ，唯一可以构成这个数组的 base[n] 对应的 n = 1，可以看出数组是 base[1] = [1, 1] 的一个排列。所以答案为 true 。</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [3, 4, 4, 1, 2, 1]\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>因为数组的最大元素是 4 ，唯一可以构成这个数组的 base[n] 对应的 n = 4 。但是 base[n] 有 5 个元素而 nums 有 6 个元素。所以答案为 false 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= num[i] &lt;= 200</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-array-is-good](https://leetcode.cn/problems/check-if-array-is-good/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-array-is-good](https://leetcode.cn/problems/check-if-array-is-good/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2, 1, 3]', '[1, 3, 3, 2]', '[1, 1]', '[3, 4, 4, 1, 2, 1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        good =  list(range(1,n)) + [n-1]\n",
    "        good.sort()\n",
    "        nums.sort()\n",
    "        return nums == good"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        m = 0\n",
    "        s = max(nums) + 1\n",
    "        if len(nums) < s:\n",
    "            return False\n",
    "        elif len(nums) == s:\n",
    "            for i in range(len(nums)):\n",
    "                if nums[i] == i + 1:\n",
    "                    m += 1\n",
    "                elif m == max(nums) and nums[-1] == nums[-2] :\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        if n == 0:\n",
    "            return False\n",
    "        nums.sort()\n",
    "\n",
    "        return nums == (list(range(1, n + 1)) + [n])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if i == n-1:\n",
    "                if nums[i] != i:\n",
    "                    return False\n",
    "            else:\n",
    "                if nums[i] != i+1:\n",
    "                    return False\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        cnt = 1\n",
    "        for i in nums:\n",
    "            if i != cnt:\n",
    "                return False\n",
    "            if cnt != n - 1:\n",
    "                cnt += 1\n",
    "        if nums[-1] == n:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        cnt = Counter(nums)\n",
    "        for i in range(1, n - 1):\n",
    "            if cnt[i] != 1:\n",
    "                return False\n",
    "        if cnt[n - 1] != 2:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = Counter(nums)\n",
    "        if cnt[n] != 2:\n",
    "            return False\n",
    "        for i in range(1, n):\n",
    "            if cnt[i] != 1:\n",
    "                return False\n",
    "        return True            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        nums.sort()\n",
    "        target = [x for x in range(1, n + 1)]\n",
    "        target.append(n)\n",
    "        return nums == target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        N = len(nums)\n",
    "        \n",
    "        for i, n in enumerate(sorted(nums)):\n",
    "            tar = min(i + 1, N - 1)\n",
    "            if n != tar:\n",
    "                return False\n",
    "        \n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        index = 0\n",
    "        while index < nums.__len__() - 1:\n",
    "            if nums[index] != (index+1):\n",
    "                return False\n",
    "            index += 1\n",
    "        return True if nums[index] == nums.__len__()-1 else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums = sorted(nums)\n",
    "        n = max(nums)\n",
    "        if len(nums) != n+1:\n",
    "            return False\n",
    "        base = list(range(1, n)) + [n, n]\n",
    "        for index, num in enumerate(nums):\n",
    "            if base[index] != num:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v]:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n = nums[-1]\n",
    "        res  = []\n",
    "        for i in range(1,n+1):\n",
    "            res.append(i)\n",
    "        res.append(n)\n",
    "        if nums == res:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums = sorted(nums)\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] != i+1:\n",
    "                return False\n",
    "        if nums[-1] != len(nums) - 1:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] != i + 1:\n",
    "                return False\n",
    "                break\n",
    "        if nums[-1] == len(nums) -1:\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "\n",
    "        n = len(nums) - 1\n",
    "        if nums[-1] != n or nums[-2] != n:\n",
    "            return False\n",
    "        \n",
    "        for i in range(n - 1):\n",
    "            if nums[i] != i + 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        if len(nums) < 2:\n",
    "            return False\n",
    "\n",
    "\n",
    "        nums.sort()\n",
    "        if len(nums) != nums[-2] and nums[-2] != nums[-1]:\n",
    "            return False\n",
    "        \n",
    "        for i in range(len(nums) -1):\n",
    "            if nums[i] != i + 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = max(nums)\n",
    "\n",
    "        if len(nums) != n+1:\n",
    "            return False\n",
    "\n",
    "        else:\n",
    "            nums.sort()\n",
    "\n",
    "            for i in range(len(nums)):\n",
    "                if nums[i] != min(i+1, n):\n",
    "                    return False\n",
    "            \n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        res = [0] * (n+1)\n",
    "        # print(res)\n",
    "        for x in nums:\n",
    "            if x > n or x < n and res[x] > 0:\n",
    "                return False\n",
    "            res[x] += 1\n",
    "        if res[-1] != 2:\n",
    "            return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        nums_len = len(nums)\n",
    "        data  = list(range(1, nums_len))\n",
    "        data.append(nums_len-1)\n",
    "        return data == nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        return sorted(nums) == list(range(1,len(nums)))+[len(nums)-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        return sorted(nums) == (list(range(1, n)) + [n - 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n=max(nums)\n",
    "        x=[i for i in range(1,n+1)]+[n]\n",
    "        return x==nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)-1\n",
    "        cont = Counter(nums)\n",
    "        if cont[n] != 2:\n",
    "            return False\n",
    "        for i in range(1,n):\n",
    "            if cont[i] != 1:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = max(nums)\n",
    "        if len(nums) != n+1:\n",
    "            return False\n",
    "        print(nums.count(n))\n",
    "        if nums.count(n) != 2:\n",
    "            return False\n",
    "        return sum(nums) == (n*(n+1)/2+n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        l = len(nums)\n",
    "        for i in range(l-1):\n",
    "            if i + 1 != nums[i]:\n",
    "                return False\n",
    "        return nums[-1] + 1 == l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = Counter(nums)\n",
    "        return cnt[n] == 2 and all(cnt[i] == 1 for i in range(1, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        f = [-1] * (n-1)\n",
    "        for x in nums:\n",
    "            if x > n-1:\n",
    "                return False\n",
    "            f[x-1] += 1\n",
    "        return f[-1] == 1 and not any(f[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        return nums==[i for i in range(1,nums[-1]+1)]+[nums[-1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        ans = False\n",
    "        _size = len(nums)\n",
    "        if _size >= 2:\n",
    "            nums.sort(reverse=False)\n",
    "            if nums[-1] == nums[-2]:\n",
    "                tmp = [i for i in range(1, nums[-1])]\n",
    "                if tmp == nums[:-2]:\n",
    "                    ans = True\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        if len(nums) == 1:\n",
    "            return False\n",
    "        nums.sort()\n",
    "        n = len(nums) - 1\n",
    "        # print(list(range(1, n)), n)\n",
    "        return nums[:n-1] == list(range(1, n)) and nums[-1] == nums[-2] == n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        ma = nums[-1]\n",
    "        n = len(nums)\n",
    "        if n <= ma:\n",
    "            return False\n",
    "        for i in range(n - 2):\n",
    "            if nums[i] == nums[i + 1] or i+1 != nums[i]:\n",
    "                return False \n",
    "        return nums[-1] == nums[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        ans = False\n",
    "        _size = len(nums)\n",
    "        if _size >= 2:\n",
    "            nums.sort(reverse=False)\n",
    "            if nums[-1] == nums[-2]:\n",
    "                tmp = [i for i in range(1, nums[-1])]\n",
    "                if tmp == nums[:-2]:\n",
    "                    ans = True\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        if n <= 1:\n",
    "            return False\n",
    "        \n",
    "        nums.sort()\n",
    "        crt = 1\n",
    "        for i,num in enumerate(nums):\n",
    "            if num != crt:\n",
    "                return False\n",
    "            else:\n",
    "                if i < n - 2:\n",
    "                    crt += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        ans = False\n",
    "        _size = len(nums)\n",
    "        if _size >= 2:\n",
    "            nums.sort(reverse=False)\n",
    "            if nums[-1] == nums[-2]:\n",
    "                tmp = [i for i in range(1, nums[-1])]\n",
    "                if tmp == nums[:-2]:\n",
    "                    ans = True\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums=sorted(nums)\n",
    "        n=len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i]!=i+1:\n",
    "                return False\n",
    "        return nums[-1]==n-1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = Counter(nums)\n",
    "        return cnt[n] == 2 and all(cnt[i] == 1 for i in range(1, n))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v]:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        counter = Counter(nums)\n",
    "        n = len(nums) - 1\n",
    "        for i in range(1, n):\n",
    "            if counter[i] != 1:\n",
    "                return False\n",
    "        return True if counter[n] == 2 else False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        a = max(nums)\n",
    "        if nums.count(a)==2:\n",
    "            if a==1:\n",
    "                return True\n",
    "            else:\n",
    "                for i in range(1,a):\n",
    "                    if nums.count(i)!=1:\n",
    "                        return False\n",
    "                    if i==a-1:\n",
    "                        return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        for i, v in enumerate(nums):\n",
    "            if i == v == len(nums) - 1:\n",
    "                return True\n",
    "            if i + 1 != v:\n",
    "                return False\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)-1):\n",
    "            if i + 1 != nums[i]:\n",
    "                return False\n",
    "        return nums[-1] + 1 == len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        for idx, num in enumerate(nums):\n",
    "            if idx != n - 1:\n",
    "                if idx != num - 1:\n",
    "                    return False\n",
    "            else:\n",
    "                if idx == num:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = max(nums)\n",
    "        nums.sort()\n",
    "        res = [i for i in range(1, n+1)] + [n]\n",
    "        return res == nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        new=[]\n",
    "        for i in range(1,max(nums)+1):\n",
    "            new.append(i)\n",
    "        else:\n",
    "            new.append(max(nums))\n",
    "        if nums==new:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v]:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v]:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums_max = max(nums) \n",
    "        nums_sum = sum(nums)\n",
    "        length = len(nums)\n",
    "        \n",
    "        if length != nums_max + 1:\n",
    "            return False\n",
    "        nums.sort()\n",
    "        print(nums)\n",
    "        for i in range(1,length-1):\n",
    "            if nums[i] != nums[i-1] + 1:\n",
    "                return False\n",
    "        if nums[length-1] != nums[length-2]:\n",
    "            return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        new=[]\n",
    "        for i in range(1,max(nums)+1):\n",
    "            new.append(i)\n",
    "        else:\n",
    "            new.append(max(nums))\n",
    "        if nums==new:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v]:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        if len(nums)<2:\n",
    "            return False\n",
    "        nums.sort()\n",
    "        for index, data in enumerate(nums[:len(nums)-1]):\n",
    "            # print(index,data)\n",
    "            if index+1 !=data:\n",
    "                return False\n",
    "        if nums[len(nums)-1]==nums[len(nums)-2]:\n",
    "            print( nums[len(nums)-1])\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)-1\n",
    "        counter=dict()\n",
    "        for i in range(1,n):\n",
    "            counter[i]=1\n",
    "        counter[n]=2\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] in counter:\n",
    "                counter[nums[i]]-=1\n",
    "        for key in counter:\n",
    "            if counter[key]!=0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        res = [i for i in range(1,n)]\n",
    "        res.append(n - 1)\n",
    "        return nums == res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v]:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums_len = len(nums)\n",
    "        if nums_len<2:\n",
    "            return False\n",
    "        nums.sort()\n",
    "        \n",
    "        for i in range(nums_len-1):\n",
    "\n",
    "            if i+1!= nums[i]:\n",
    "                return False\n",
    "        if nums[-1]== nums[-2] and nums_len-1 == nums[-1]:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        s = set()\n",
    "        \n",
    "        for num in nums:\n",
    "            if num > n:\n",
    "                return False\n",
    "            if num in s and num != n-1:\n",
    "                return False\n",
    "\n",
    "            s.add(num)\n",
    "\n",
    "        return sum(nums) == (n*(n-1)/2 + n-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        for i, x in enumerate(nums):\n",
    "            if i < len(nums) - 1:\n",
    "                if x != i + 1:\n",
    "                    return False\n",
    "        return nums[-1] == n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n = max(nums)\n",
    "        target = [i for i in range(1,n+1)] + [n]\n",
    "        return nums == target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        l=[i for i in range(1,nums[-1]+1)]+[nums[-1]]\n",
    "        n1=len(nums)\n",
    "        n2=len(l)\n",
    "        if n1!=n2:\n",
    "            return False\n",
    "        for i in range(len(l)):\n",
    "            if l[i]!=nums[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        if len(nums) == 1:\n",
    "            return False\n",
    "        nums.sort()\n",
    "        n = len(nums) - 1\n",
    "        # print(list(range(1, n)), n)\n",
    "        return nums[:n-1] == list(range(1, n)) and nums[-1] == nums[-2] == n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        tar = (len(nums) + 1 )* len(nums) // 2 - 1\n",
    "        max_n = 0\n",
    "        unique = set()\n",
    "        for i in nums:\n",
    "            tar -= i \n",
    "            unique.add(i)\n",
    "            max_n = max(i,max_n)\n",
    "        return tar==0 and max_n==len(nums)-1 and len(unique) == max_n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def isGood(self, nums: List[int]) -> bool:\n",
    "    n = len(nums) - 1\n",
    "    nc = 0\n",
    "    bits = [False] * (n + 2)\n",
    "    for i in nums:\n",
    "      if i > n:\n",
    "        return False\n",
    "      elif i == n:\n",
    "        nc += 1\n",
    "        if nc > 2:\n",
    "          return False\n",
    "      elif not bits[i]:\n",
    "        bits[i] = True\n",
    "      elif bits[i]:\n",
    "        return False\n",
    "    return True\n",
    "        \n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        nums_len = len(nums)\n",
    "\n",
    "        for i in range(0, nums_len):\n",
    "        \n",
    "            if i == nums_len - 1:\n",
    "                if not nums[i] == nums_len - 1:\n",
    "                    return False\n",
    "            elif nums[i] != i + 1:\n",
    "                return False\n",
    "\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        # 抽屉原理\n",
    "        n = len(nums) - 1\n",
    "        cnt = Counter()\n",
    "        for v in nums:\n",
    "            if v > n or v < n and cnt[v] or v == n and cnt[v] == 2:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n=max(nums)\n",
    "        base=list(range(1,n+1))\n",
    "        base.append(n)\n",
    "        return sorted(nums)==base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        return sorted(nums) == list(range(1, max(nums) + 1)) + [max(nums)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: list[int]) -> bool:\n",
    "        n, cnt = len(nums), Counter(nums)\n",
    "        return len(cnt) == n - 1 and max(nums) == n - 1 and cnt[n - 1] == 2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v] > 0:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        m = max(nums)\n",
    "        nums.remove(m)\n",
    "        return sorted(nums) == list(range(1, m + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        t = list(set(nums))\n",
    "        n = len(nums)\n",
    "        if len(t) < n - 1:\n",
    "            return False\n",
    "        elif nums.count(max(t)) != 2 or max(t) > n or min(t) != 1:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n=max(nums)\n",
    "        base=list(range(1,n+1))\n",
    "        base.append(n)\n",
    "        return sorted(nums)==base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = [0] * (n + 1)\n",
    "        for v in nums:\n",
    "            if v > n or v == n and cnt[v] > 1 or v < n and cnt[v]:\n",
    "                return False\n",
    "            cnt[v] += 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        n = len(nums) - 1\n",
    "        cnt = Counter(nums)\n",
    "        return cnt[n] == 2 and all(cnt[i] == 1 for i in range(1, n))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        a = max(nums)\n",
    "        standard_list = list(range(1,a+1))+[a]\n",
    "        nums.sort()\n",
    "        if len(nums)!=len(standard_list):\n",
    "            return False\n",
    "        for c,d in zip(nums,standard_list):\n",
    "            #print(c,d)\n",
    "            if c !=d:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isGood(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        return nums == [i for i in range(1, len(nums))] + [len(nums) - 1]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
